<?php

namespace Modules\Api;

use Phalcon\DiInterface;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Mvc\View\Engine\Volt;

class Module implements ModuleDefinitionInterface {

    public function registerAutoloaders(DiInterface $di = null) {
        $loader = new Loader();
        $loader->registerNamespaces(array(
            'Modules\Api\Controllers' => __DIR__ . '/controllers/',
            //'Modules\Front\Models'      => __DIR__ . '/models/',
            //            'Models'                    => APP_PATH . '/apps/models/',
            //            'Models\Base'               => APP_PATH . '/apps/models/base/',
            //            'Lib'                       => APP_PATH . '/apps/library/Lib/',
            //            'Lib\CurlMulti'             => APP_PATH . '/apps/library/Lib/CurlMulti',
        ));

        $loader->register();
    }

    public function registerServices(DiInterface $di) {
        $di->set('volt', function ($view, $di) {
            $config = $di['config'];
            $volt   = new Volt($view, $di);

            //            $templatePath = $config->application->voltDir;
            $voltOptions = array(
                //模板编译目录
                "compiledPath"      => $config->application->voltDir,
                "compiledExtension" => '.volt',  // 生成的后缀名
                'compiledSeparator' => '__',      // 文件的
                'compileAlways'     => false,
                /*'compiledPath' => function ($templatePath) {
                    $dirName = dirname($templatePath);

                    if (!is_dir(APP_PATH . '/cache/volt/' . $dirName)) {
                        mkdir(APP_PATH . '/cache/volt/' . $dirName, "0755",true);
                    }
                    return APP_PATH . 'cache/volt/' . $dirName . '/'. $templatePath . '.volt';
                },*/
            );

            if (isset($config->application->debug) && '1' == $config->application->debug) {
                // 模板是否实时编译
                $voltOptions['compileAlways'] = true;
            }

            $volt->setOptions($voltOptions);

            /*$volt->getCompiler()->addFunction(
                'tr',
                function ($key) {
                    return "Bootstrap::translate({$key})";
                }
            );*/

            return $volt;
        });

        $di->set('view', function () {
            $view = new View();
            $view->setViewsDir(__DIR__ . '/views/');
            $view->registerEngines(array(
                ".phtml" => 'volt', // 后缀为phtml使用volt引擎
            ));

            return $view;
        });
        //var_dump($di->get('view'));die('123');

        /**
         * Database connection is created based in the parameters defined in the configuration file
         */
        //        $di['db'] = function () use ($config) {
        //            return new DbAdapter($config->database->toArray());
        //        };

        $di->set('dispatcher', function () use ($di) {
            $eventsManager = $di->getShared('eventsManager');

            // wechat login
            //$wxLogin = new \Plugins\WxLogin($di);
            //$eventsManager->attach('dispatch', $wxLogin);

            //$noFound       = new \Plugins\NotFound($di);
            //$eventsManager->attach('dispatch', $noFound);
            $dispatcher = new \Phalcon\Mvc\Dispatcher();
            $dispatcher->setEventsManager($eventsManager);
            $dispatcher->setDefaultNamespace('\Modules\Api\Controllers');

            return $dispatcher;
        });
    }
}
